<template>
    <div class="detailPage">
        <!-- 轮播图 -->
        <van-swipe :autoplay="3000" indicator-color="#1989fa">
          <van-swipe-item v-for="item in bannerList" :key="item.id">
            <img :src="item.img_url" alt="" width="100%" />
          </van-swipe-item>
        </van-swipe>

        <!-- tips组件 -->
        <Tips />

        <!-- 产品信息渲染和展示弹出层 -->
        <div class="info">
            <h3>{{info.name}}</h3>
            <p>{{info.goods_brief}}</p>
            <div>{{info.retail_price | RMBFormat}}</div>
        </div>
        <van-cell title="展示弹出层" is-link @click="isShowSku=true"/> 

        <!-- 商品参数 -->
        <div class="attribute">
            <h4>商品参数</h4>
            <ul>
                <li v-for="(item, index) in attribute" :key="index">
                    <span>{{item.name}}</span>
                    <section class="van-ellipsis">{{item.value}}</section>
                </li>
            </ul>
        </div>

        <!-- 产品详情图片 -->
        <div class="goods_desc" v-html="info.goods_desc"></div>

        <!-- 常见问题 -->
        <div class="title">
            <span>常见问题</span>
        </div>
        <ul class="issue">
            <li v-for="item in issue" :key="item.id">
                <h3>{{item.question}}</h3>
                <p>{{item.answer}}</p>
            </li>
        </ul>

        <!-- 相关产品 -->
        <div class="title">
            <span>相关产品</span>
            <Products :productsList="goodsList" />
        </div>

        <!-- 商品导航 -->
        <AppGoodsAction @showSku="addToCart" :goodsCount="goodsCount" @hdChanisCollected="hdChanisCollected" :isCollected="isCollected" />

        <!-- sku -->
        <van-sku
            ref="sku"
            v-model="isShowSku"
            :sku="sku"
            :goods="goods"
            :hide-stock="sku.hide_stock"
        />

    </div>
</template>

<script>
// 请求api相关
import { getGoodsDetailById,getGoodsRelatedById,getCartData,toAddCart } from "@/request/detailApi";

// 组件相关
import Tips from '@/components/common/Tips.vue'
import Products from '@/components/common/Products.vue'
import AppGoodsAction from '@/components/main/AppGoodsAction.vue'
export default {
    components: {
        Tips,
        Products,
        AppGoodsAction
    },
    data() {
        return {
            // 轮播图信息
            bannerList:[],
            // 产品信息（价格和标题）
            info:{},
            // 商品参数
            attribute:[],
            // 常见问题
            issue:[],
            // 相关产品数据
            goodsList:[],
            // 购物车数量
            goodsCount:0,
            // 产品列表数据，仅用来加入购物车功能
            productList:[],
            // 是否收藏
            isCollected: false,  //收藏功能目前还没有接口..

            //-------- sku相关配置
            // 是否显示
            isShowSku: false,
            sku: {
                hide_stock: false, //是否隐藏库存
                tree:[], //一些参数配置，这里不需要写入
                price: '1.00', // 默认价格（单位元）
                stock_num: 227, // 商品总库存
            },
            goods: {
                picture:''
            }
        }
    },
    computed: {

    },
    created() {
        // 请求详情信息
        // this.$route.query.id从路由中的query获取对应商品id
        getGoodsDetailById({
            id: this.$route.query.id
        }).then(res=>{
            let { gallery,info,attribute,issue,productList  } = res.data
            // 商品详情数据
            this.bannerList = gallery
            this.info = info
            this.attribute = attribute
            this.issue = issue
            // sku数据
            this.goods.picture = info.list_pic_url
            this.sku.price = info.retail_price
            this.sku.stock_num = info.goods_number

            this.productList = productList  //用来加入购物车时要用到产品id
        })

        // 请求相关产品
        getGoodsRelatedById({
            id: this.$route.query.id
        }).then(res=>{
            this.goodsList = res.data.goodsList
        })

        // 获取购物车数据
        getCartData().then(res=>{
            //console.log(res)
            this.goodsCount = res.data.cartTotal.goodsCount;
        })
    },
    methods: {
        addToCart(){
            // 如果没登录 跳转到登录
            if(!this.$utils.mustBeLogin()) return
                
            // 如果sku是在展示，那么就执行正真的加入购物车
            // 如果sku是隐藏，那就只是弹出窗口
            if(this.isShowSku){
                // 获取要购买的数量，这些refs操作来着官方说明
                let number =  this.$refs.sku.getSkuData().selectedNum 
                // 发起加入购物车请求
                toAddCart({
                    goodsId: this.$route.query.id,
                    productId: this.productList[0].id,
                    number
                }).then(res=>{
                    this.goodsCount = res.data.cartTotal.goodsCount

                    this.$toast.success('添加成功')
                })
            } 
            this.isShowSku = !this.isShowSku
        },
        // 收藏功能 
        hdChanisCollected(){
            if(!this.$utils.mustBeLogin()) return
            
            this.isCollected = !this.isCollected
        }
    }
}
</script>

<style lang="less" scoped>
.detailPage{
    padding-bottom: .5rem;
}
.info{
    background: #fff;
    text-align: center;
    padding: .2rem 0;
    h3{
        font-weight: normal;
        font-size: .2rem;
        line-height: .5rem;
    }
    p{
        color: #999;
        font-size: .14rem;
        line-height: .3rem;
    }
    div{
        color: darkred;
        font-size: .14rem;
        line-height: .3rem;
    }
}
.attribute{
    margin-top: .2rem;
    background: #fff;
    padding: .2rem 4%;
    h4{
        font-weight: normal;
        font-size: .16rem;
        margin-bottom: 0.2rem;
    }
    ul{
        li{
            background: #efefef;
            line-height: .4rem;
            height: .4rem;
            margin-bottom: .1rem;
            border-radius: 4px;
            display: flex;
            justify-content: space-between;
            span{
                width: 30%;
                text-align: right;
            }
            section{
                flex: 1;
                padding-left: .2rem;
            }
        }
    }
}
/* 如果样式无法替换，可以试试/deep/ 深度样式写法 */
/deep/.goods_desc{
    img{max-width:100%; display: block;}
}
.title {
  width: 100%;
  background: #fff;
  height: 0.5rem;
  position: relative;
  &::before {
    content: "";
    width: 50%;
    height: 2px;
    background: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -1px;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    position: relative;
    background: #fff;
    display: block;
    text-align: center;
    margin: 0 auto;
    height: 0.5rem;
    line-height: 0.5rem;
  }
}
.issue {
  padding: 0 4%;
  background: #fff;
  li {
    h3 {
      height: 0.3rem;
      line-height: 0.3rem;
      padding-left: 0.2rem;
      position: relative;
      font-weight: normal;
      &::before {
        content: "";
        width: 4px;
        height: 4px;
        background: darkred;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 50%;
        margin-top: -2px;
      }
    }
    p {
      font-size: 0.12rem;
      line-height: 0.2rem;
      padding-left: 0.2rem;
    }
  }
}
/deep/.van-sku-messages{
  padding: 0;
}
.van-sku-container{
  min-height: auto;
}
</style>
